Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruff rules for comprehensions and performance #168

Merged
merged 3 commits into from
Dec 17, 2024

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Dec 15, 2024

% ruff check --select=C4,C90,PERF --statistics

3	C408   	[*] unnecessary-collection-call
3	C901   	[ ] complex-structure
3	PERF401	[ ] manual-list-comprehension
1	C403   	[*] unnecessary-list-comprehension-set
1	C419   	[*] unnecessary-comprehension-in-call
[*] fixable with `ruff check --fix`

In micropip/externals/mousebender/simple.py, Mypy is a bit behind pyupgrade on Python 3.12.

% ruff rule C408

unnecessary-collection-call (C408)

Derived from the flake8-comprehensions linter.

Fix is always available.

What it does

Checks for unnecessary dict(), list() or tuple() calls that can be
rewritten as empty literals.

Why is this bad?

It's unnecessary to call, e.g., dict() as opposed to using an empty
literal ({}). The former is slower because the name dict must be
looked up in the global scope in case it has been rebound.

Examples

dict()
dict(a=1, b=2)
list()
tuple()

Use instead:

{}
{"a": 1, "b": 2}
[]
()

Fix safety

This rule's fix is marked as unsafe, as it may occasionally drop comments
when rewriting the call. In most cases, though, comments will be preserved.

Options

  • lint.flake8-comprehensions.allow-dict-calls-with-keyword-arguments

Copy link
Member

@ryanking13 ryanking13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Looks good codewise, could you resolve the conflict?

@cclauss
Copy link
Contributor Author

cclauss commented Dec 16, 2024

Should we exclude micropip/externals/mousebender/simple.py given that it is external?

Mypy is a bit behind pyupgrade on Python 3.12 in that file.

@ryanking13
Copy link
Member

@cclauss

Yes, please exclude the external directory. They are mostly copy/pasted.

Copy link
Member

@ryanking13 ryanking13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ryanking13 ryanking13 merged commit fe21dd7 into pyodide:main Dec 17, 2024
6 checks passed
@cclauss cclauss deleted the ruff_0.8.3 branch December 17, 2024 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants